package com.itheima.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.commons.template.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.ContactVo;
import com.itheima.domain.vo.MessageCommentVO;
import com.itheima.domain.vo.PageResult;
import com.itheima.dubbo.api.AnnouncementApi;
import com.itheima.dubbo.api.CommentApi;
import com.itheima.dubbo.api.FriendApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/2/2 22:09
 */
@Service
public class MessageService {

    @Reference
    private FriendApi friendApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private AnnouncementApi announcementApi;


    public ResponseEntity contactWithStranger(Long friendId) {

        // 查找当前用户
        Long userId = UserHolder.getUserId();

        // 调用环信api,添加好友
        huanXinTemplate.contactUsers(userId, friendId);
        // 保存到MongoDB中
        friendApi.addFriend(userId, friendId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findFriend(int page, int pagesize, String keyword) {
        // 调用api查询
        PageResult<Friend> pageResult = friendApi.findFriendList(page, pagesize, UserHolder.getUserId(), keyword);
        List<ContactVo> voList = new ArrayList<>();
        for (Friend friend : pageResult.getItems()) {
            ContactVo vo = new ContactVo();

            UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setUserId(userInfo.getId().toString());
            vo.setCity(StringUtils.substringBefore(userInfo.getCity(), "-"));

            voList.add(vo);
        }

        PageResult<ContactVo> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }

    public ResponseEntity findLikePerson(int commentType, int page, int pagesize) {
        PageResult<Comment> pageResult = commentApi.findCommentByUser(page, pagesize, commentType, UserHolder.getUserId());
        List<MessageCommentVO> voList = new ArrayList<>();
        for (Comment comment : pageResult.getItems()) {
            MessageCommentVO vo = new MessageCommentVO();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setId(userInfo.getId().toString());
            vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(comment.getCreated())));

            voList.add(vo);
        }

        PageResult<MessageCommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }


    public ResponseEntity findAnnouncements(int page, int pagesize) {
        IPage<Announcement> iPage = announcementApi.findAnnouncementList(page, pagesize);
        List<AnnouncementVo> voList = new ArrayList<>();
        for (Announcement announcement : iPage.getRecords()) {
            AnnouncementVo vo = new AnnouncementVo();
            vo.setId(announcement.getId().toString());
            vo.setTitle(announcement.getTitle());
            vo.setDescription(announcement.getDescription());
            vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(announcement.getCreated()));

            voList.add(vo);
        }

        PageResult<AnnouncementVo> voPageResult = new PageResult<>();
        voPageResult.setPage(page);
        voPageResult.setItems(voList);
        voPageResult.setPagesize(pagesize);
        voPageResult.setPages((int)iPage.getPages());
        voPageResult.setCounts((int) iPage.getTotal());
        return ResponseEntity.ok(voPageResult);
    }
}
